// Copyright 2022 a.o.smith ALL Rights Reserved
package com.liuxinlong.modules.material.controller;

import com.liuxinlong.annotation.LogAnnotation;
import com.liuxinlong.common.ImportResp;
import com.liuxinlong.common.UploadService;
import com.liuxinlong.constant.LogBusinessConstants;
import com.liuxinlong.constant.LogOperateConstants;
import com.liuxinlong.modules.common.controller.request.BaseDeleteReq;
import com.liuxinlong.modules.common.controller.request.QueryBaseReq;
import com.liuxinlong.modules.common.controller.response.BaseResp;
import com.liuxinlong.modules.entity.MaterialConvolution;
import com.liuxinlong.modules.material.controller.request.AddMaterialConvolutionReq;
import com.liuxinlong.modules.material.controller.request.ImportMaterialConvolutionReq;
import com.liuxinlong.modules.material.controller.request.QueryConvolutionErrorReq;
import com.liuxinlong.modules.material.controller.request.QueryConvolutionStepReq;
import com.liuxinlong.modules.material.controller.request.QueryMaterialConvolutionReq;
import com.liuxinlong.modules.material.controller.request.UpdateConvolutionConfigReq;
import com.liuxinlong.modules.material.controller.request.UpdateConvolutionStatusReq;
import com.liuxinlong.modules.material.service.MaterialConvolutionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartRequest;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 物料成本卷积控制层
 *
 * @author liuxinlong@hotwater.com.cn
 * @version 1.0.0
 * @since 2023年5月16日
 */
@RestController
@RequestMapping("convolution")
public class MaterialConvolutionController {
    @Autowired
    private MaterialConvolutionService materialConvolutionService;

    @Autowired
    private UploadService uploadService;

    /**
     * 分页查询物料成本卷积记录
     *
     * @param req 请求信息
     * @return 物料成本卷积记录列表
     */
    @PostMapping("page")
//    @LogAnnotation(value = "分页查询", business = LogBusinessConstants.MATERIAL_CONVOLUTION, operateType = LogOperateConstants.OPERATE_QUERY)
    public BaseResp pageMaterialConvolutionRecord(@RequestBody QueryMaterialConvolutionReq req) {
        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("startNum", req.getStartNum());
        queryMap.put("pageSize", req.getPageSize());
        queryMap.put("materialNo", req.getMaterialNo());
        queryMap.put("materialName", req.getDesc());
        queryMap.put("region", req.getRegion());
        queryMap.put("status", req.getStatus());
        queryMap.put("operateUser",req.getOperateUser());
        List<Map<String, Object>> dataList = materialConvolutionService.pageMaterialConvolutionList(queryMap);
        int totalNum = materialConvolutionService.getConvolutionRecordCount(queryMap);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("分页查询物料成本卷积记录成功");
        resp.setDataList(dataList);
        resp.setTotalNum(totalNum);
        return resp;
    }

    /**
     * 查询物料信息
     *
     * @param req 请求信息
     * @return 物料信息
     */
    @PostMapping("material-get")
//    @LogAnnotation(value = "分页查询", business = LogBusinessConstants.MATERIAL_CONVOLUTION, operateType = LogOperateConstants.OPERATE_QUERY)
    public BaseResp getMaterialInfo(@RequestBody QueryBaseReq req) {
        Map<String, Object> dataMap = materialConvolutionService.getOneMaterial(req.getId());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("查询物料信息成功");
        resp.setDataMap(dataMap);
        return resp;
    }

    /**
     * 新增物料成本卷积信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("add")
    @LogAnnotation(value = "新增物料成本卷积信息", business = LogBusinessConstants.MATERIAL_CONVOLUTION, operateType = LogOperateConstants.OPERATE_ADD)
    public BaseResp addMaterialConvolution(@RequestBody AddMaterialConvolutionReq req) {
        MaterialConvolution materialConvolution = new MaterialConvolution();
//        if (!StringUtils.equals(req.getRegion(),"SWTNJ")) {
//            throw new AosException(ReturnCodes.HTTP_PARAM_ERROR,"由于公司职能整合数据迁移，6/28日起原热水器工厂&水系统工厂成本卷积暂停使用，请关注！");
//        }
        materialConvolution.setRegion(req.getRegion());
        materialConvolution.setMaterialNo(req.getMaterial());
        materialConvolution.setCopyUser(req.getCopyUser());
        materialConvolution.setRemark(req.getRemark());
        materialConvolutionService.addMaterialConvolution(materialConvolution);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("新增物料成本卷积信息成功");
        return resp;
    }


    /**
     * 删除物料成本卷积信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("delete")
    @LogAnnotation(value = "删除物料成本卷积信息", business = LogBusinessConstants.MATERIAL_CONVOLUTION, operateType = LogOperateConstants.OPERATE_DELETE)
    public BaseResp deleteMaterialConvolution(@RequestBody BaseDeleteReq req) {
        materialConvolutionService.deleteMaterialConvolution(req.getId());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("删除物料成本卷积信息成功");
        return resp;
    }

    /**
     * 导入数据预处理
     *
     * @param multipartRequest 文件
     * @return 数据
     */
    @PostMapping("import")
    @LogAnnotation(value = "导入数据预处理", business = LogBusinessConstants.MATERIAL_CONVOLUTION, operateType = LogOperateConstants.OPERATE_IMPORT)
    public ImportResp importConvolutions(MultipartRequest multipartRequest) {
        Map<String, Object> map = uploadService.uploadFileLocal(multipartRequest);
        ImportResp resp = materialConvolutionService.importConvolutions((String) map.get("fileRealPath"));
        resp.setDataMap((Map<String, Object>) map.get("otherParameter"));
        return resp;
    }

    /**
     * 导入物料成本卷积信息
     *
     * @param req 请求体
     * @return 返回导入失败数据
     */
    @PostMapping("batch-add")
    @LogAnnotation(value = "导入物料成本卷积数据", business = LogBusinessConstants.MATERIAL_CONVOLUTION, operateType = LogOperateConstants.OPERATE_IMPORT)
    public ImportResp batchAddConvolution(@RequestBody ImportMaterialConvolutionReq req) {
        return materialConvolutionService.batchAddConvolution(req.getDataList(),req.getOtherParameter());
    }

    /**
     * 导出物料成本卷积信息
     *
     * @param response response
     */
    @PostMapping({"export"})
    @LogAnnotation(value = "导出物料成本卷积数据", business = LogBusinessConstants.MATERIAL_CONVOLUTION, operateType = LogOperateConstants.OPERATE_EXPORT)
    public void exportMaterialConvolution(HttpServletResponse response) {
        materialConvolutionService.exportMaterialConvolution(response);
    }

    /**
     * 变更物料成本卷积信息状态
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("status-update")
    @LogAnnotation(value = "物料成本卷积信息状态变更", business = LogBusinessConstants.MATERIAL_CONVOLUTION, operateType = LogOperateConstants.OPERATE_MODIFY)
    public BaseResp updateMaterialConvolutionStatus(@RequestBody UpdateConvolutionStatusReq req) {
        materialConvolutionService.updateMaterialConvolutionStatus(req.getId(), req.getOperate(), req.getTargetStatus());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("物料成本卷积信息状态变更成功");
        return resp;
    }

    /**
     * 查询物料成本卷积配置信息
     *
     * @return 成功标识
     */
    @GetMapping("config-query")
//    @LogAnnotation(value = "查询物料成本卷积配置信息", business = LogBusinessConstants.MATERIAL_CONVOLUTION, operateType = LogOperateConstants.OPERATE_MODIFY)
    public BaseResp queryConvolutionConfig() {
        Map<String, Object> dataMap = materialConvolutionService.queryConvolutionConfig();
        BaseResp resp = new BaseResp();
        resp.setDataMap(dataMap);
        resp.setRetMessage("查询物料成本卷积配置信息成功");
        return resp;
    }

    /**
     * 修改物料成本卷积配置信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("config-update")
    @LogAnnotation(value = "修改物料成本卷积配置信息", business = LogBusinessConstants.MATERIAL_CONVOLUTION, operateType = LogOperateConstants.OPERATE_MODIFY)
    public BaseResp updateConvolutionConfig(@RequestBody UpdateConvolutionConfigReq req) {
        materialConvolutionService.updateConvolutionConfig(req.getWpcUser(), req.getWscUser(), req.getSwtUser());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("修改物料成本卷积配置信息成功");
        return resp;
    }

    /**
     * 查询物料成本卷积信息
     *
     * @param req 请求信息
     * @return 物料成本卷积信息
     */
    @PostMapping("query")
//    @LogAnnotation(value = "分页查询", business = LogBusinessConstants.MATERIAL_CONVOLUTION, operateType = LogOperateConstants.OPERATE_QUERY)
    public BaseResp getConvolutionInfo(@RequestBody QueryBaseReq req) {
        Map<String, Object> dataMap = materialConvolutionService.getConvolutionInfo(req.getId());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("查询物料成本卷积信息成功");
        resp.setDataMap(dataMap);
        return resp;
    }

    /**
     * 查询物料成本卷积失败信息
     *
     * @param req 请求信息
     * @return 物料成本卷积失败信息
     */
    @PostMapping("error")
//    @LogAnnotation(value = "分页查询", business = LogBusinessConstants.MATERIAL_CONVOLUTION, operateType = LogOperateConstants.OPERATE_QUERY)
    public BaseResp getErrorList(@RequestBody QueryConvolutionErrorReq req) {
        List<Map<String, Object>> dataList = materialConvolutionService.getErrorList(req.getId(),req.getProcessId());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("查询物料成本卷积失败信息成功");
        resp.setDataList(dataList);
        return resp;
    }

    /**
     * 查询物料成本卷积流程信息
     *
     * @param req 请求信息
     * @return 物料成本卷积信息
     */
    @PostMapping("process")
//    @LogAnnotation(value = "分页查询", business = LogBusinessConstants.MATERIAL_CONVOLUTION, operateType = LogOperateConstants.OPERATE_QUERY)
    public BaseResp getConvolutionProcessInfo(@RequestBody QueryBaseReq req) {
        List<Map<String, Object>> dataList = materialConvolutionService.getConvolutionProcessInfo(req.getId());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("查询物料成本卷积流程信息成功");
        resp.setDataList(dataList);
        return resp;
    }

    /**
     * 查询物料成本卷积操作记录信息
     *
     * @param req 请求信息
     * @return 物料成本卷积信息
     */
    @PostMapping("step")
    public BaseResp getConvolutionStepInfo(@RequestBody QueryConvolutionStepReq req) {
        Map<String,Object> queryParam = new HashMap<>();
        queryParam.put("startNum",req.getStartNum());
        queryParam.put("pageSize",req.getPageSize());
        queryParam.put("convolutionId",req.getId());
        queryParam.put("node",req.getNode());
        List<Map<String, Object>> dataList = materialConvolutionService.getConvolutionStepInfo(queryParam);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("查询物料成本卷积操作记录信息成功");
        resp.setDataList(dataList);
        return resp;
    }

}
